<template>
	<view>
		<view class="bg_orderDetail">
			<view class="padding text-white rowsb align-center align-center">
			</view>
			<view class="padding text-white">
				待付款
			</view>
		</view>
		<view class="bg-white padding margin radius align-center " style="margin-top: -150rpx;" @click="addressMen()">
			<view class="align-center">
				<view>
					<image src="../../static/dingwei.png" mode="" style="width: 30rpx;height: 30rpx;"></image>
				</view>
				<view class="margin-left-sm">
					<view>{{AddressList.name}}</view>
					<view class="onelineshow">
						地址{{AddressList.sca_name}}{{AddressList.address}}
					</view>
				</view>
				<view class="margin-left" v-if="AddressList==''">
					请选择收货地址
				</view>
			</view>
			<!-- 商城下单数据 -->
		</view>
		<view>
			<shoppingOrder :orderDetail="orderDetail" :total='total' v-if="orderInfo=='false'"></shoppingOrder>
			<shopOrder :orderDetail='orderDetail' v-else :image='image'></shopOrder>
		</view>
		<view style="height: 140rpx;"></view>
		<view class="bg-white padding-sm position-bottom justify-end solid-top" @click="orderPayMen">
			<view class="border padding-lr padding-tb-sm margin-left round" style="color: #235099;border: 1rpx solid #235099;">立即支付</view>
		</view>
	</view>
</template>

<script>
	const app = getApp().globalData
	import {
		getAllAddress,
		addOrderData
	} from '../../common/path.js'
	import shopOrder from '../shop/shopDetail/components/shopOrder.vue'
	import shoppingOrder from '../shop/shopDetail/components/shoppingOrder.vue'
	export default {
		components: {
			shopOrder,
			shoppingOrder
		},
		data() {
			return {
				AddressList: [],
				orderDetail: [],
				remark: '', //备注
				orderInfo: '',
				image: '',
				payType:2
			}
		},
		onLoad(option) {
			this.image = app.image
			this.orderDataMen(option)
		},
		onShow() {
			this.getAllAddress() //获取地址列表接口
		},

		methods: {
			//确认订单
			orderDataMen(option) {
				if(option.detail){
				this.orderDetail = JSON.parse(option.detail)
				}
				this.orderInfo = option.orderInfo //判断是否是从购物车进入
				this.total = option.total //总价
				console.log('传过来的参数', this.orderDetail)
			},
			//获取地址接口
			getAllAddress() {
				let data = {
					id: app.userInfo.id,
				}
				this.$axios.axios('POST', getAllAddress, data)
					.then(res => {
						if (res.status == 1) {
							res.data.forEach(item => {
								if (item.is_default == 1) {
									this.AddressList = item
									// console.log('默认地址',item)
								}
							})
							console.log('默认地址====>', this.AddressList)
						}
					})
					.catch(err => {
						console.log(err)
					})
			},
			//商品下单
			addOrderData() {
				let orderIdList = []
				let data = {}
				let setdataarrList, setdataarr
				if (this.orderInfo == 'false') {
					this.orderDetail.forEach(item => {
						orderIdList.push(item.id)
						setdataarr = Array.from(new Set(orderIdList))
						setdataarrList = setdataarr.join(',')
					})
					console.log('拿到的id====>', setdataarrList)
					data = {
						user_id: app.userInfo.id,
						cart_ids: setdataarrList,
						address_id: this.AddressList.id,
						pay_type: this.payType, //支付方式
						type: 1, //快递 1  自提是2
					}
				} else {
					data = {
						user_id: this.orderDetail.user_id,
						cart_ids: this.orderDetail.cart_ids,
						goods_id: this.orderDetail.goods_id,
						spec: this.orderDetail.spec,
						num: this.orderDetail.num,
						address_id: this.AddressList.id,
						pay_type: this.payType, //支付方式
						type: 1, //快递 1  自提是2
						pick_name: '', //收货人姓名 ，自提用
						pick_tel: '', //收件人手机号，自提使用
					}
				}
				console.log('获取到的data====>', data)
				this.$axios.axios('POST', addOrderData, data)
					.then(res => {
						console.log('下单成功', res)
						if (res.status == 1) {
							this.$tools.showToast(res.msg)
							if(this.payType ==2){
							//支付宝
								this.zfbPay(res.data)
							}else{
								this.wechatPay(res.data)
							}
							//跳转
						} else {
							this.$tools.showToast(res.msg)
						}
					})
					.catch(err => {
						console.log(err)
					})
			},
			// 支付弹窗
			orderPayMen() {
				let that = this
				uni.showActionSheet({
					itemList: ['支付宝支付', '微信支付'],
					success: function(res) {
						if (res.tapIndex == 0) {
							// 支付宝支付
							that.payType = 2
							that.addOrderData()
						} else {
							//微信支付
							that.payType = 1 
							that.addOrderData()
						}
						console.log('选中了第' + (res.tapIndex + 1) + '个按钮');
						console.log('选中了第>>>>>>>>>>', res)
					},
					fail: function(res) {
						console.log(res.errMsg);
					}
				});
			},
			//微信支付
			zfbPay(data) {
				console.log('[支付宝]',data);
				uni.requestPayment({
					provider: 'alipay',
					orderInfo: data,
					success: (res) => {
						setTimeout(r => {
							uni.reLaunch({
								url: '../my/my/my'
							})
						}, 1000)
					},
					fail: (res) => {
						console.log(res, 'fail');
						this.$tools.showToast('支付拉起失败')
					}
				})
			},
			wechatPay(data) {
				console.log(data, '微信支付');
				uni.requestPayment({
					provider: 'wxpay',
					orderInfo: JSON.parse(data),
					success: (res) => {
						setTimeout(r => {
							uni.reLaunch({
								url: '../my/my/my'
							})
						}, 1000)
					},
					fail: (res) => {
						console.log(res, 'fail');
						this.$tools.showToast('微信支付拉起失败')
					}
				})
			},
			addressMen() {
				uni.navigateTo({
					url: '/pages/address/address'
				})
			}
		},
		//跳转地址管理



	}
</script>

<style>
	.bg_orderDetail {
		background-color: #235099;
		height: 350rpx;
	}

	.position-bottom {
		position: fixed;
		z-index: 1;
		left: 0;
		right: 0;
		bottom: 0;
	}
</style>
